<template>
  <div class="app_main">
    <div class="search_box">
      <el-form ref="searchForm" :inline="true" :model="searchForm" size="mini">
        <search-form-box :show-more-btn="false">
          <template slot="showSearch">
            <el-form-item label="日期" prop="startDate">
              <el-date-picker
                v-model="searchForm.startDate"
                type="date"
                placeholder="选择开始日期"
                value-format="yyyy-MM-dd"
                format="yyyy年MM月dd日"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="至" prop="endDate">
              <el-date-picker
                v-model="searchForm.endDate"
                type="date"
                placeholder="选择结束日期"
                value-format="yyyy-MM-dd"
                format="yyyy年MM月dd日"
              >
              </el-date-picker>
            </el-form-item>
            <el-form-item label="客户" prop="customer_id">
              <el-select v-model="searchForm.customer_id" clearable filterable>
                <el-option v-for="item in customerArr" :key="item.id" :value="item.id" :label="item.name"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="项目" prop="building_area_id">
              <el-select v-model="searchForm.building_area_id" clearable filterable>
                <el-option v-for="item in buildingAreaArr" :key="item.id" :value="item.id" :label="item.name"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="工单类型" prop="work_order_type_id">
              <el-select v-model="searchForm.work_order_type_id" clearable filterable>
                <el-option value="1" label="新安装"></el-option>
                <el-option value="2" label="上刊"></el-option>
                <el-option value="3" label="下刊"></el-option>
                <!--                <el-option value="4" label="巡检"></el-option>-->
                <el-option value="5" label="维修"></el-option>
                <el-option value="7" label="换画"></el-option>
                <el-option value="8" label="中间监播"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="工人" prop="worker_id">
              <el-select v-model="searchForm.worker_id" clearable filterable>
                <el-option v-for="item in workerArr" :key="item.id" :value="item.id" :label="item.name"></el-option>
              </el-select>
            </el-form-item>
          </template>
          <template slot="searchBtn">
            <el-form-item>
              <el-button size="mini" type="primary" plain @click="search">查询</el-button>
              <el-button size="mini" type="info" plain @click="resetForm('searchForm')">重置</el-button>
            </el-form-item>
          </template>
        </search-form-box>
      </el-form>
    </div>
    <div class="data_box">
      <el-table v-loading="tableLoading" :data="tableData" border size="mini">
        <el-table-column fixed="left" width="50" label="序号" align="center">
          <template slot-scope="scope">
            <span>{{ (listParams.page - 1) * listParams.page_size + scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column label="日期" width="120" prop="date" align="center"></el-table-column>
        <el-table-column label="客户名称" min-width="120" show-overflow-tooltip align="center">
          <template slot-scope="scope">
            {{ scope.row.customer_id ? scope.row.customer_name : '无' }}
          </template>
        </el-table-column>
        <el-table-column label="项目名称" min-width="200" prop="building_area_name" show-overflow-tooltip align="center"
        ></el-table-column>
        <el-table-column label="动作" width="100" prop="work_order_type_name" align="center"></el-table-column>
        <el-table-column label="总数" width="100" prop="total_num" align="center"></el-table-column>
        <el-table-column label="已执行" width="100" prop="executed_num" align="center"></el-table-column>
        <el-table-column label="未执行" width="100" prop="unexecuted_num" align="center"></el-table-column>
        <el-table-column label="最近提交时间" width="200" prop="last_submit_time" align="center">
          <template slot-scope="scope">
            {{ scope.row.last_submit_time ? scope.row.last_submit_time : '暂无数据' }}
          </template>
        </el-table-column>
        <el-table-column label="负责人" width="120" prop="worker_name" align="center"></el-table-column>
      </el-table>
    </div>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page.sync="searchForm.page"
      :page-sizes="[10, 20, 50, 100, 200, 400]"
      :page-size.sync="searchForm.page_size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      style="margin-top: 10px"
    >
    </el-pagination>
  </div>
</template>

<script>
import SearchFormBox from '@/components/SearchFormBox'
import { company_list_all } from '@/api/customer/customer'
import { listWorker } from '@/api/workOrder/workOrder'
import { list_all_simple } from '@/api/building/building'
import { installCountRecord } from '@/api/workOrder/workSubmit'

export default {
  name: 'record',
  components: { SearchFormBox },
  data() {
    return {
      tableData: [],
      tableLoading: false,
      searchForm: {
        page: 1,
        page_size: 10,
        // 开始时间
        startDate: null,
        // 结束时间
        endDate: null,
        // 工人id
        worker_id: null,
        // 楼盘id
        building_area_id: null,
        // 工单类型ID
        work_order_type_id: null,
        // 客户ID
        customer_id: null
      },
      listParams: {
        page: 1,
        page_size: 10,
        // 开始时间
        startDate: null,
        // 结束时间
        endDate: null,
        // 工人id
        worker_id: null,
        // 楼盘id
        building_area_id: null,
        // 工单类型ID
        work_order_type_id: null,
        // 客户ID
        customer_id: null
      },
      total: null,
      workerArr: [],
      buildingAreaArr: [],
      customerArr: []
    }
  },
  created() {
    this.initCustomerArr()
    this.initWorkerArr()
    this.initBuildingAreaArr()
    this.getList()
  },
  methods: {
    handleSizeChange(val) {
      this.listParams.page_size = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.listParams.page = val
      this.getList()
    },
    getList() {
      this.tableLoading = true
      installCountRecord(this.listParams).then(res => {
        this.tableLoading = false
        this.total = res.data.total
        this.tableData = res.data.records
      }).catch(() => {
        this.tableLoading = false
      })
    },
    search() {
      this.searchForm.page = 1
      this.listParams = Object.assign({}, this.searchForm)
      this.getList()
    },
    resetForm(formName) {
      this.$refs[formName].resetFields()
      this.listParams = Object.assign({}, this.searchForm)
      this.getList()
    },
    initCustomerArr() {
      company_list_all({}).then(res => {
        this.customerArr = res.data
      })
    },
    initWorkerArr() {
      listWorker().then(res => {
        this.workerArr = res.data
      })
    },
    initBuildingAreaArr() {
      list_all_simple().then(res => {
        this.buildingAreaArr = res.data
      })
    }
  }
}
</script>

<style scoped lang="scss">
.app_main {
  padding: 20px 20px 0 20px;
}
</style>
